package com.aote.gradleproject.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

/**
 * 所有的json工具类
 * @author 张琪
 * @date 2021/7/16 16:52
 */
@Slf4j
public final class Json {
    private static ObjectMapper mapper = new ObjectMapper();

    /**
     * 对象转json
     * @param object 需要转换的pojo对象
     * @return 转换后的JSONObject对象
     */
    public static JSONObject object2Json(Object object){
        JSONObject json = null;
        try {
            String s = mapper.writeValueAsString(object);
            json = new JSONObject(s);
        } catch (JsonProcessingException e) {
            log.warn("对象转换json失败, 请检查json格式");
            e.printStackTrace();
        }
        return json;
    }
}
